<template>
  <el-card>
    <template #header>
      <span>FILES</span>
    </template>
    <el-table :data="that.tableData" stripe border height="calc(100vh - 120px)" style="width: 100%">
      <el-table-column type="index" fixed="left" width="50" align="center"></el-table-column>
      <el-table-column label="预览" width="150px" align="center">
        <template #default="scope">
          <el-link type="primary" :href="viewFile(scope.row)" target="_blank">
            <el-image :src="viewFile(scope.row) + '?t=' + Date.now()" style="width: 100px;"></el-image>
          </el-link>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间"></el-table-column>
      <el-table-column label="ID/扩展名">
        <template #default="scope">
          <p>UserId：{{ scope.row.userId }}</p>
          <p>FileId：{{ scope.row.fileId }}</p>
          <p>扩展名：{{ scope.row.extname }}</p>
        </template>
      </el-table-column>
      <el-table-column fixed="right" width="100px" align="center">
        <template #header>
          <el-upload action="#" :http-request="httpRequest" :show-file-list="false">
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
        </template>
        <template #default="scope">
          <el-upload action="#" :http-request="(params) => { httpRequest(params, scope.row) }" :show-file-list="false">
            <el-button size="small" type="text">重传</el-button>
          </el-upload>
          <el-button type="text" class="color-danger" @click="remove(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>

<script setup>
import {fileList, fileRemove, preview, upload} from "../../assets/api";
import {reactive} from "vue";

const that = reactive({
  tableData: []
})

const httpRequest = (params, row) => {
  let formData = new FormData();
  formData.append('file', params.file);
  if (row) {
    formData.append('fileId', row.fileId);
  }
  upload(formData).then(({data}) => {
    list();
  })
}

// 预览
const viewFile = (row) => {
  return preview(row);
}

// 文件删除
const remove = (row) => {
  fileRemove(row).then(({data})=>{
    list();
  })
}

// 文件列表
const list = () => {
  fileList().then(({data})=>{
    that.tableData = data;
  })
}

list()
</script>

<style scoped>

</style>
